package com.amanda.controller;

import com.amanda.constant.MessageConstant;
import com.amanda.entity.Result;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author amanda
 * @Description 用于展示用户名
 */

@RequestMapping("/user")
@RestController
public class UserController {
    @RequestMapping("/getUsername")
    public Result getUsername() {
        // 完成认证之后，会将当前用户信息保存到security框架提供的上下文对象，基于session
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (user != null) {
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS, user.getUsername());
        }
        return new Result(false, MessageConstant.GET_USERNAME_FAIL);
    }
}
